import { Application } from "egg";
import { Schema } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface TestProps {
  name: string;
  age: number;
  createDat: Date;
  updateDat: Date;
}
function initTestModel(app: Application) {
  // 使用mongoose-sequence插件函数实现ID自增长
  const AutoIncrement = AutoIncrementFactory(app.mongoose);
  // 创建数据模型
  const testSchema = new Schema<TestProps>(
    {
      name: { type: String, unique: true, required: true },
      age: { type: String, required: true },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  testSchema.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Test_counter",
  });
  return app.mongoose.model<TestProps>("Test", testSchema);
}
export default initTestModel;
